package com.quanyan.ic.vendingMachine.dao.impl;

import com.quanyan.ic.common.paging.Limit;
import com.quanyan.ic.vendingMachine.dao.VmOrderDAO;
import com.quanyan.ic.vendingMachine.http.request.ReqSalesRecordCondition;
import com.quanyan.ic.vendingMachine.mapper.VmOrderEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.VmOrderItemEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.customSQL.VmReportMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.VmOrderEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmOrderEntityExample;
import com.quanyan.ic.vendingMachine.mapper.entity.VmOrderItemEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmOrderItemEntityExample;
import com.quanyan.ic.vendingMachine.model.GoodsInfo;
import com.quanyan.ic.vendingMachine.model.OrderInfo;
import com.quanyan.ic.vendingMachine.model.OrderItem;
import com.quanyan.ic.vendingMachine.model.SalesRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/8/2.
 */
@Repository
public class VmOrderDAOImpl implements VmOrderDAO {

    @Autowired
    private VmReportMapper vmReportMapper;
    @Autowired
    private VmOrderEntityMapper vmOrderEntityMapper;
    @Autowired
    private VmOrderItemEntityMapper vmOrderItemEntityMapper;

    @Override
    public List<SalesRecord> querySalesRecordList(ReqSalesRecordCondition condition) {
        return vmReportMapper.querySalesRecordList(
                condition, Limit.build(condition.getPageNum(), condition.getPageSize()));
    }

    @Override
    public int countSalesRecordList(ReqSalesRecordCondition condition) {
        return vmReportMapper.countSalesRecordList(condition);
    }

    private VmOrderEntity findVmOrderEntity(String orderNo) {
        VmOrderEntityExample example = new VmOrderEntityExample();
        example.createCriteria()
                .andOrderNoEqualTo(orderNo);
        List<VmOrderEntity> orderEntities = vmOrderEntityMapper.selectByExample(example);
        if (orderEntities == null || orderEntities.size() <= 0) {
            return null;
        }
        return orderEntities.get(0);
    }

    @Override
    public OrderInfo findOrder(String orderNo) {
        if (StringUtils.isEmpty(orderNo)) {
            return null;
        }
        VmOrderEntity orderEntity = findVmOrderEntity(orderNo);
        if (orderEntity == null) {
            return null;
        }
        VmOrderItemEntityExample orderItemEntityExample = new VmOrderItemEntityExample();
        orderItemEntityExample.createCriteria()
                .andOrderNoEqualTo(orderNo)
                .andVmNumberEqualTo(orderEntity.getVmNumber());
        List<VmOrderItemEntity> orderItemEntities = vmOrderItemEntityMapper.selectByExample(orderItemEntityExample);
        return toOrderModel(orderEntity, orderItemEntities);
    }

    @Override
    public OrderItem findOrderItem(String orderNo, String vmNumber, Byte gridNumber) {
        if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(vmNumber)
                || gridNumber == null) {
            return null;
        }
        VmOrderItemEntityExample example = new VmOrderItemEntityExample();
        example.createCriteria()
                .andOrderNoEqualTo(orderNo)
                .andVmNumberEqualTo(vmNumber)
                .andGridNumberEqualTo(gridNumber);
        List<VmOrderItemEntity> orderItemEntities = vmOrderItemEntityMapper.selectByExample(example);
        if (orderItemEntities == null || orderItemEntities.size() <= 0) {
            return null;
        }
        return toOrderItemModel(orderItemEntities.get(0));
    }

    @Transactional
    @Override
    public void insertOrder(OrderInfo orderInfo) {
        VmOrderEntity vmOrderEntity = new VmOrderEntity();
        BeanUtils.copyProperties(orderInfo, vmOrderEntity);
        List<OrderItem> orderItems = orderInfo.getItems();
        if (orderItems == null || orderItems.size() <= 0) {
            return;
        }
        vmOrderEntityMapper.insertSelective(vmOrderEntity);
        for (OrderItem orderItem : orderItems) {
            vmOrderItemEntityMapper.insertSelective(toOrderItemEntity(orderItem));
        }
    }

    @Override
    public void updateOrderPayInfo(String orderNo, String buyer, Byte payWay, Byte payStatus, Long payCallbackTime) {
        if (StringUtils.isEmpty(orderNo)) {
            return;
        }
        VmOrderEntity orderEntity = findVmOrderEntity(orderNo);
        if (orderEntity == null) {
            return;
        }
        VmOrderEntity vmOrderEntity = new VmOrderEntity();
        vmOrderEntity.setId(orderEntity.getId());
        vmOrderEntity.setBuyer(buyer);
        vmOrderEntity.setPayWay(payWay);
        vmOrderEntity.setPayStatus(payStatus);
        vmOrderEntity.setPayCallbackTime(new Date(payCallbackTime));
        vmOrderEntityMapper.updateByPrimaryKeySelective(vmOrderEntity);
    }

    @Override
    public void updateOrderItemShipInfo(Integer orderItemId, Byte shipStatus, Byte shipQty, Long shipCallbackTime) {
        VmOrderItemEntity entity = new VmOrderItemEntity();
        entity.setId(orderItemId);
        entity.setShipStatus(shipStatus);
        entity.setShipQty(shipQty);
        entity.setShipCallbackTime(new Date(shipCallbackTime));
        vmOrderItemEntityMapper.updateByPrimaryKeySelective(entity);
    }

    private VmOrderItemEntity toOrderItemEntity(OrderItem orderItem) {
        VmOrderItemEntity orderItemEntity = new VmOrderItemEntity();
        BeanUtils.copyProperties(orderItem, orderItemEntity);
        GoodsInfo goodsInfo = orderItem.getGoodsInfo();
        orderItemEntity.setGoodsId(goodsInfo.getId());
        orderItemEntity.setGoodsUuid(goodsInfo.getUuid());
        orderItemEntity.setGoodsName(goodsInfo.getName());
        orderItemEntity.setGoodsPrice(new BigDecimal(goodsInfo.getPrice()));
        orderItemEntity.setGoodsCategoryId(goodsInfo.getCategoryId());
        orderItemEntity.setGoodsCategory(goodsInfo.getCategoryName());
        orderItemEntity.setQty(orderItem.getQty());
        return orderItemEntity;
    }

    private OrderItem toOrderItemModel(VmOrderItemEntity entity) {
        OrderItem orderItem = new OrderItem();
        BeanUtils.copyProperties(entity, orderItem);
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(entity.getGoodsId());
        goodsInfo.setUuid(entity.getGoodsUuid());
        goodsInfo.setCategoryId(entity.getGoodsCategoryId());
        goodsInfo.setCategoryName(entity.getGoodsCategory());
        goodsInfo.setName(entity.getGoodsName());
        goodsInfo.setPrice(entity.getGoodsPrice().doubleValue());
        orderItem.setGoodsInfo(goodsInfo);
        return orderItem;
    }

    private OrderInfo toOrderModel(VmOrderEntity orderEntity, List<VmOrderItemEntity> orderItemEntities) {
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(orderEntity, orderInfo);
        List<OrderItem> items = new ArrayList<>();
        if (orderItemEntities == null || orderItemEntities.size() <= 0) {
            orderInfo.setItems(items);
            return orderInfo;
        }
        for (VmOrderItemEntity orderItemEntity : orderItemEntities) {
            items.add(toOrderItemModel(orderItemEntity));
        }
        orderInfo.setItems(items);
        return orderInfo;
    }
}
